package com.ml.structure.linkedlist;

public class HeroList {
	private HeroNode head = new HeroNode(0, "");

	// 从尾部添加节点
	public void add(HeroNode heroNode) {
		HeroNode temp = head;
		while (true) {
			if (temp.getNext() == null) {
				break;
			}
			temp = temp.getNext();
		}
		temp.setNext(heroNode);
	}

	// 遍历链表
	public void showList(HeroList h1) {
		if (head.getNext() == null) {
			System.out.println("链表为空");
		}
		HeroNode temp = head;
		while (true) {
			if (temp.getNext() == null) {
				break;
			}
			temp = temp.getNext();
			System.out.println(temp);
		}
	}

	// 根据序号，从中间添加节点，需要相同继续添加在原始后面
	public void addByOrder(HeroNode h1) {
		HeroNode temp = head;
		while (true) {
			if (temp.getNext() == null) {
				temp.setNext(h1);
				break;
			}
			temp = temp.getNext();
			if (temp.getNo() == h1.getNo() && temp.getNext() == null) {
				temp.setNext(h1);
				break;
			} else if (temp.getNo() == h1.getNo() && temp.getNext() != null) {
				h1.setNext(temp.getNext());
				temp.setNext(h1);
				break;
			} else if (temp.getNo() > h1.getNo() && temp.getNext() == null) {
				temp.setNext(h1);
				break;
			} else if (temp.getNo() > h1.getNo() && temp.getNext() != null) {
				h1.setNext(temp.getNext());
				temp.setNext(h1);
				break;
			}

		}
	}

	// 根据序号，从中间添加节点，需要相同覆盖
	public void addByOrder2(HeroNode h1) {
		HeroNode temp = head;
		while (true) {
			if (temp.getNext() == null) {
				temp.setNext(h1);
				break;
			}
			if (temp.getNext().getNo() == h1.getNo() && temp.getNext().getNext() == null) {
				temp.setNext(h1);
				break;
			} else if (temp.getNext().getNo() == h1.getNo() && temp.getNext() != null) {
				h1.setNext(temp.getNext().getNext());
				temp.setNext(h1);
				break;
			} else if (temp.getNext().getNo() > h1.getNo() && temp.getNext().getNext() == null) {
				temp.setNext(h1);
				break;
			} else if (temp.getNext().getNo() > h1.getNo() && temp.getNext().getNext() != null) {
				h1.setNext(temp.getNext());
				temp.setNext(h1);
				break;
			}
			temp = temp.getNext();
		}
	}
	//删除节点
	public void deleteNode(HeroNode h1){
		HeroNode temp = head;
		while(true){
			if(temp.getNext()==null){
				System.out.println("链表为空");
				break;
			}
			if(temp.getNext().getNo()==h1.getNo()&&temp.getNext().getNext()==null){
				temp.setNext(null);
				break;
			}else if(temp.getNext().getNo()==h1.getNo()&&temp.getNext().getNext()!=null){
				temp.setNext(temp.getNext().getNext());
				break;
			}
			temp=temp.getNext();
		}
	}
}
